/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hirt;

import java.nio.ByteBuffer;
import java.util.Vector;

/**
 * RemoveAction are used to remove actions from the scene
 * It is used when a vehicle leaves the scene of the editor wants the item gone
 * It can also be used to remove a group items of a specified type from an items itemList
 * @author zamille
 */
public class RemoveAction extends Action
{
    int amount;
    String type;

    /**
     *
     * @param The Item that will be removed
     */
    public RemoveAction(Item actor)
    {
        super (actor);
        super.messageType = Message.REMOVE_MESSAGE;
        amount = 0;
        type = "";
    }

    /**
     *
     * @param Owner of removed items
     * @param type of item that needs removing
     * @param amount that will be removed.  Must be greator than 1
     */
    public RemoveAction(Item actor, String type, int amount)
    {
        super (actor);
        super.messageType = Message.REMOVE_MESSAGE;
        this.amount = amount;
        this.type = type;
    }

    /**
     * Builds an Action from a byte stream, presumably created by an action across the network
     * this action should be identical to that one
     * @param bytes
     * @param items: Master Item List used to build the action
     */
    public RemoveAction(ByteBuffer bytes, Vector<Item> items)
    {
        fromBytes(bytes, items);
        super.messageType = Message.REMOVE_MESSAGE;
    }

    /**
     * Builds an Action from a save string, presumably read from a save
     * this action should be identical to the one saved in the file
     * @param save string
     * @param items: Master Item List used to build the action
     * @return what's left of the parem SaveString after this methods completes
     */
    public RemoveAction(String text, Vector<Item> items)
    {
        super(text, items);
        super.messageType = Message.REMOVE_MESSAGE;
    }

    /**
     * Returns a ByteBuffer representation of this Action
     * Used to send actions over the network
     * @return ByteBuffer
     */
    @Override
    public ByteBuffer toByte()
    {
        ByteBuffer buf =  super.toByte();
        buf.putInt(amount);
        buf.putInt(type.length());
        buf.put(ByteChanger.stringToByte(type));
        return buf;
    }

    /**
     * Returns a String representation of this Action
     * Used to save this action to a file
     * @return Save String
     */
    @Override
    protected ByteBuffer fromBytes(ByteBuffer bytes, Vector<Item> items)
    {
        bytes = super.fromBytes(bytes, items);   //it uses the inherited method to put
        amount = bytes.getInt(0);               //the inherited members from the ByteBuffer
        type = ByteChanger.getString(bytes, 4);
        try {
            bytes = ByteChanger.truncate(bytes, 4+type.length());
        } catch (Exception ex) {

        }
        return bytes;
    }

    /**
     * Returns a String representation of this Action
     * Used to save this action to a file
     * @return Save String
     */
    @Override
    public String toSaveString()
    {
        String text = super.toSaveString();   //it uses the inherited method to put
        text += amount + "\t";          //the inherited members in the Save String
        text += type + "\t";
        return text;
    }

    /**
     * Builds an Action from a save string, presumably read from a save
     * this action should be identical to the one saved in the file
     * @param save string
     * @param items: Master Item List used to build the action
     * @return what's left of the parem SaveString after this methods completes
     */
     @Override
    protected String fromSaveString(String text, Vector<Item> items)
    {
        text = super.fromSaveString(text, items);   //it uses the inherited method to build
        String substring = ByteChanger.getNextValue(text);  //the inherited members from the Save String
        amount = Integer.parseInt(substring);

        text = ByteChanger.incrementString(text);
        substring = ByteChanger.getNextValue(text);
        type = substring;

        text = ByteChanger.incrementString(text);
        return text;
    }
}


